import { EMAIL, PHONE, IDCARD } from "@/utils/regEx";

/**
 * @description 验证手机号码
 * @param mobile 手机号码
 * @returns 布尔值
 */
export function validateMobile(mobile: string): boolean {
  let regex = /^1[3456789]\d{9}$/;
  return regex.test(mobile);
}

/**
 * 邮箱
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email: string) {
  const reg = EMAIL;
  return reg.test(email);
}

/**
 * 手机
 * @param {string} mobile
 * @returns {Boolean}
 */
export function validMobile(mobile: string) {
  const reg = PHONE;
  if (!reg.test(mobile)) {
    showError("手机号格式不正确");
    return false;
  }
  return true;
}

/**
 * 身份证号
 * @param {string} idcard
 * @returns {Boolean}
 */
export function validIdcard(idcard: string) {
  const reg = IDCARD;
  if (!reg.test(idcard)) {
    showError("证件号格式不正确");
    return false;
  }
  return true;
}

function showError(message: string) {
  uni.showToast({
    title: message,
    icon: "none",
    duration: 2000,
  });
}
/**
 * 函数防抖 (延迟执行，只执行最后一次点击)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const Debounce = (fn:Function, t?:number) => {
	const delay = t || 500
	let timer:any
	return function(this:unknown,...args: any[]) {
		if (timer) {
			clearTimeout(timer)
		}
		timer = setTimeout(() => {
			console.log("执行");
			timer = null
			fn.apply(this, args)
		}, delay)
	}
}
/**
 * 函数防抖 (立即执行，只执行最后一次点击)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const debounceImmediate = (fn:Function, t?:number) => {
  const delay = t || 500
  let timer:  any = null;

  let immediateExecuted = false;
  return function (this: unknown, ...args: any[]) {
    if (!immediateExecuted) {
      fn.apply(this, args)
      immediateExecuted = true;
    }
    clearTimeout(timer)

    timer = setTimeout(() => {
      immediateExecuted = false
    }, delay)
  }
}
/**
 * 函数节流
 * @param fn
 * @param interval
 * @returns {Function}
 * @constructor
 */
export const Throttle = (fn:Function, t?:number) => {
	let last:any
	let timer:any
	const interval = t || 500
	return function(this:unknown,...args:any[]) {
		const now = +new Date()
		if (last && now - last < interval) {
			clearTimeout(timer)
			timer = setTimeout(() => {
				last = now
				fn.apply(this, args)
			}, interval)
		} else {
			last = now
			fn.apply(this, args)
		}
	}
}
/**
 * 更新小程序
 * @return void
 */
export function updateMp() {
  const updateManager = wx.getUpdateManager();
  const startParamObj = wx.getEnterOptionsSync();
  if (wx.canIUse("getUpdateManager") && startParamObj.scene != 1154) {
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function (res:any) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        updateManager.onUpdateFailed(function () {
          uni.showToast({
            title: "新版本下载失败",
          })
        });
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: "更新提示",
            content: "新版本已经下载好，是否重启当前应用？",
            success(res:any) {
              if (res.confirm) {
                updateManager.applyUpdate();
              }
            },
          });
        });
        updateManager.onUpdateFailed(function () {
          wx.showModal({
            title: "发现新版本",
            content: "请删除当前小程序，重启搜索打开...",
          });
        });
      }
    });
  }
}
